package com.heye.imheye.security;

import com.heye.imheye.entity.User;
import com.heye.imheye.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Slf4j
@RequiredArgsConstructor
public class MyUserDetailsService implements UserDetailsService {

    private final UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.queryByUsername(username);
        if (user == null) {
            log.debug("Query returned no results for user '" + username + "'");
            throw new UsernameNotFoundException("Query returned no results for user '" + username + "'");
        }
        return new MyUserDetails(user);
    }
}
